﻿using NoteHelper.IBusiness;
using NoteHelper.Model;
using NoteHelper.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace NoteHelper.Account
{
	public class Acfl001Controller
	{
		private int rowIndex;
		private IBaseView view;
        private IAccountFlowManager accountFlowManager;
        public Acfl001Controller(IBaseView baseView, IAccountFlowManager manaer)
		{
			view = baseView;
            accountFlowManager = manaer;

            view.Insert += Form_Insert;
			view.Edit += Form_Edit;
			view.Remove += Form_Remove;
			view.Copy += Form_Copy;
			view.Query += Form_Query;
			view.Accept += Form_Accept;
			view.Cancel += Form_Cancel;
			view.Print += Form_Print;
			view.Help += Form_Help;
            Form_Cancel(null, null);
            view.EndEdit();
            var ht = new Hashtable();
            ht.Add("HappenDate", DateTime.Today);
            Query(ht);
		}
		private void Form_Insert(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Insert;
			view.BeginEdit();
		}
		private void Form_Edit(object sender, EventArgs e)
		{
			BeginEdit(FormCommand.Edit);
		}
		private void Form_Remove(object sender, EventArgs e)
        {
            Form_Cancel(null, null);
            rowIndex = view.GetRowIndex();
            if (rowIndex == -1) return;

            var deleteRow = view.DisplayList.Rows[rowIndex];
            var flowId = deleteRow["Id"];
            accountFlowManager.Delete(flowId);
            view.DisplayList.Rows.RemoveAt(rowIndex);
            for (int i = rowIndex; i < view.DisplayList.Rows.Count; i++)
            {
                var row = view.DisplayList.Rows[i];
                row["$_rowIndex"] = i;
            }            
		}
		private void Form_Copy(object sender, EventArgs e)
		{
			BeginEdit(FormCommand.Insert);
		}
		private void BeginEdit(FormCommand cmd)
		{
			rowIndex = view.GetRowIndex();
			if (rowIndex == -1) return;

            var selectedRow = view.DisplayList.Rows[rowIndex];
            var ht = view.RowData;
            ht.GetValueFormDataRow(selectedRow);
			view.RowData = ht;
			view.Cmd = cmd;
			view.BeginEdit();
		}
		private void Form_Query(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Query;
			view.BeginEdit();
		}
		private void Form_Accept(object sender, EventArgs e)
        {
            var ht = new Hashtable();
            bool error = false;
            switch (view.Cmd)
            {
                case FormCommand.Display:
                    break;
                case FormCommand.Insert:
                    var insertEntity = new Model.AccountFlow();
                    view.RowData.FillValueToEntity(insertEntity);
                    insertEntity.HappenDate = insertEntity.HappenDate.Date;
                    accountFlowManager.Add(insertEntity);

                    ht.Add("FromDate", insertEntity.HappenDate.AddDays(-3));
                    ht.Add("AccountId", insertEntity.AccountId);
                    Query(ht);
                    break;
                case FormCommand.Edit:
                    var editRow = view.DisplayList.Rows[rowIndex];
                    var flowId = editRow["Id"];
                    var editEntity = accountFlowManager.UpdateFlow(flowId, view.RowData);
                    ht = new Hashtable();
                    ht.Add("FromDate", editEntity.HappenDate.AddDays(-3));
                    ht.Add("AccountId", editEntity.AccountId);
                    Query(ht);
                    break;
                case FormCommand.Query:
                    Query(view.RowData);
                    break;
                default:
                    break;
            }
            if (error) return;
            view.RowData = null;
            view.Cmd = FormCommand.Display;
            view.EndEdit();
        }
        private void Query(Hashtable ht)
        {
            var table = accountFlowManager.QueryAccountFlow(ht);
            table.Columns.Add("$_rowIndex", typeof(int));
            for (int i = 0; i < table.Rows.Count; i++)
            {
                var row = table.Rows[i];
                row["$_rowIndex"] = i;
            }
            view.DisplayList = table;
        }
        private void Form_Cancel(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Display;
			view.EndEdit();
		}
		private void Form_Print(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}
		private void Form_Help(object sender, EventArgs e)
		{
            AlterMsg.Info("试下：acfl002");
		}
	}
}
